/*
 * @(#) $Header$
 *
 * Copyright (C) 2008  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderedImageFactory;
import javax.media.jai.ROI;
import ca.forklabs.media.jai.operator.PrecisionAndRecallDescriptor;

/**
 * Class {@code PrecisionAndRecallRIF} is a {@link RenderedImageFactory}
 * supporting the <em>precision</em>, <em>recall</em> and
 * <em>precision+recall</em> operation in the rendered image layers.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.opimage.PrecisionAndRecallRIF">Daniel Léonard</a>
 * @version $Revision$
 * @see    PrecisionAndRecallOpImage
 */
public class PrecisionAndRecallRIF implements RenderedImageFactory {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public PrecisionAndRecallRIF() {
   // nothing
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets class of the candidate image.
    * @param   pb   the parameter block.
    * @return   the class.
    */
   protected int[] getCandidateColor(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.CANDIDATE_COLOR_PARAMETER_INDEX;
      int[] color = (int[]) pb.getObjectParameter(index);
      return color;
      }

   /**
    * Gets the truth image.
    * @param   pb   the parameter block.
    * @return   the truth image.
    */
   protected RenderedImage getTruthImage(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.TRUTH_IMAGE_PARAMETER_INDEX;
      RenderedImage image = (RenderedImage) pb.getObjectParameter(index);
      return image;
      }

   /**
    * Gets class of the truth image.
    * @param   pb   the parameter block.
    * @return   the class.
    */
   protected int[] getTruthColor(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.TRUTH_COLOR_PARAMETER_INDEX;
      int[] color = (int[]) pb.getObjectParameter(index);
      return color;
      }

   /**
    * Get the region of interest.
    * @param   pb   the parameter block.
    * @return   the region of interest.
    */
   protected ROI getRegionOfInterest(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.REGION_OF_INTEREST_PARAMETER_INDEX;
      ROI roi = (ROI) pb.getObjectParameter(index);
      return roi;
      }

   /**
    * Get the horizontal sampling rate.
    * @param   pb   the parameter block.
    * @return   the horizontal sampling rate.
    */
   protected int getHorizontalSamplingRate(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.HORIZONTAL_SAMPLING_PARAMETER_INDEX;
      int sampling_rate = pb.getIntParameter(index);
      return sampling_rate;
      }

   /**
    * Get the vertical sampling rate.
    * @param   pb   the parameter block.
    * @return   the vertical sampling rate.
    */
   protected int getVerticalSamplingRate(ParameterBlock pb) {
      int index = PrecisionAndRecallDescriptor.VERTICAL_SAMPLING_PARAMETER_INDEX;
      int sampling_rate = pb.getIntParameter(index);
      return sampling_rate;
      }


//---------------------------
// Implemented methods from java.awt.image.renderable.RenderedImageFactory
//---------------------------

   /**
    * Creates a new instance of {@link PrecisionAndRecallOpImage} in the
    * rendered layer.
    * @param   pb   the parameter block.
    * @param   hints   ignored.
    */
   @Override
   public RenderedImage create(ParameterBlock pb, RenderingHints hints) {
      RenderedImage candidate = pb.getRenderedSource(0);
      int[] candidate_color = this.getCandidateColor(pb);

      RenderedImage truth = this.getTruthImage(pb);
      int[] truth_color = this.getTruthColor(pb);

      ROI roi = this.getRegionOfInterest(pb);
      int h_start = candidate.getMinX();
      int v_start = candidate.getMinY();
      int h_sample = this.getHorizontalSamplingRate(pb);
      int v_sample = this.getHorizontalSamplingRate(pb);

      RenderedImage sink = new PrecisionAndRecallOpImage(candidate, candidate_color, truth, truth_color,
                                                         roi,
                                                         h_start, v_start,
                                                         h_sample, v_sample);
      return sink;
      }

   }
